//
//  GAOEmoticonKeyboard.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let emoticonCollectionViewCellIdentifier = "emoticonCollectionViewCellIdentifier"

class GAOEmoticonKeyboard: UIView {

    
    // 表情键盘的Toolbar
    private lazy var toolBar: GAOEmoticonToolBar = {
        let toolBar = GAOEmoticonToolBar(frame: CGRectZero)
        return toolBar
    }()
    // 表情视图
    private lazy var emoticonCollectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        view.backgroundColor = RandomColor()
        view.registerClass(GAOEmoticonCell.self, forCellWithReuseIdentifier: emoticonCollectionViewCellIdentifier)
        //开启分页模式
        view.pagingEnabled = true
        //水平方向滚动
        flowLayout.scrollDirection = .Horizontal
        //关闭滚动条
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        //关闭回弹效果
        view.bounces = false
        
        view.dataSource = self
        view.delegate = self
        return view
    }()
    
    
    //分页控件
    private lazy var pageControl: UIPageControl = {
        let ctr = UIPageControl()
        ctr.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        ctr.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        ctr.numberOfPages = 6
        return ctr
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        toolBar.didSelectedEmoticonButtonClosure = {[weak self] (type: GAOEmoticonToolBarButtonType) in
            let indexPath: NSIndexPath
            switch type {
            case .Normal:
                    print("normal")
                indexPath = NSIndexPath(forItem: 0, inSection: 0)
            case .Emoji:
                    print("emoji")
                indexPath = NSIndexPath(forItem: 0, inSection: 1)
            case .Lxh:
                    print("lxh")
                indexPath = NSIndexPath(forItem: 0, inSection: 2)
            }
            //根据选择的表情类型,滚动到相应的表情视图
            self?.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)
            //  设置pagecontrol数据
            self?.setPageControlData(indexPath)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addSubview(emoticonCollectionView)
        addSubview(toolBar)
        addSubview(pageControl)
        //  默认选中表情类型的pageControl
        let indexPath: NSIndexPath = NSIndexPath(forItem: 0, inSection: 0)
        setPageControlData(indexPath)
        
        
        //设置约束
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(40)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
            make.height.equalTo(10)
        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
       let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        
        flowLayout.itemSize = emoticonCollectionView.size
        //水平垂直间距
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
    }
    //设置PageControl数据
    private func setPageControlData(indexPath: NSIndexPath) {
        pageControl.numberOfPages = GAOEmoticonTools.shareTools.allEmoticons[indexPath.section].count
        pageControl.currentPage = indexPath.item
    
    }
    
}

extension GAOEmoticonKeyboard: UICollectionViewDataSource {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return GAOEmoticonTools.shareTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return GAOEmoticonTools.shareTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(emoticonCollectionViewCellIdentifier, forIndexPath: indexPath) as! GAOEmoticonCell
        cell.backgroundColor = backgroundColor
        //给cell的表情数据模型数据赋值
        cell.emoticons = GAOEmoticonTools.shareTools.allEmoticons[indexPath.section][indexPath.item]
        cell.indexPath = indexPath
        return cell
        
    }
}

extension GAOEmoticonKeyboard: UICollectionViewDelegate {
   
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //当前x方向偏移量
        let offsetX = scrollView.contentOffset.x
        //获取经过当前可视区域中点位置的indexPath(此indexPath对应的cell就是将要显示的cell)
        
        if let indexPath = emoticonCollectionView.indexPathForItemAtPoint(CGPointMake(offsetX + emoticonCollectionView.width / 2.0, 0)) {
            print(indexPath.section)
            print(CGPointMake(offsetX + emoticonCollectionView.width / 2.0, 0))
            toolBar.selectEmoticonTypeButtonWithSection(indexPath.section)
            
            setPageControlData(indexPath)
        }
       
        
    }
}
